import { handleFont } from "@common";
import { supportedSites } from "@config";
import handlers from "./handlers";

import { injectLink } from "@utils";

let url = document.location.href;

//判断当前地址是否是想要的网站
const is = (url, domain) => url.search(domain) !== -1;

Object.entries(supportedSites).forEach((item) => {
  const [site_name, { url: site_url }] = item;
  if (site_url !== undefined && is(url, site_url)) {
    //看看是否有对应的handler存在
    handlers.hasOwnProperty(site_name)
      ? handlers[site_name].handler((doc) => {
          handleFont(doc, (fontface) => {
            injectLink(
              doc,
              chrome.runtime.getURL(`assets/css/${site_name}.css`)
            );
          });
        })
      : "";
  }
});
